home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libobj / list.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  3KB  |  150 lines

  1. /*
  2.  * list.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: list.c,v 4.0 91/07/17 14:38:42 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    list.c,v $
  19.  * Revision 4.0  91/07/17  14:38:42  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "geom.h"
  24. #include "list.h"
  25.  
  26. static Methods *iListMethods = NULL;
  27. static char listName[] = "list";
  28.  
  29. List *
  30. ListCreate()
  31. {
  32.     return (List *)share_calloc(1, sizeof(List));
  33. }
  34.  
  35. char *
  36. ListName()
  37. {
  38.     return listName;
  39. }
  40.  
  41. /*
  42.  * Take a list whose DATA field points to a linked list of objects and
  43.  * turn it into a List.
  44.  */
  45. int
  46. ListConvert(list, objlist)
  47. List *list;
  48. Geom *objlist;
  49. {
  50.     int num;
  51.  
  52.     /*
  53.      * Find the unbounded objects on the list as well as the
  54.      * bounding box of the list.
  55.      */
  56.     list->list = objlist;
  57.     for (num = 0; objlist; objlist = objlist->next)
  58.         num += objlist->prims;
  59.     return num;
  60. }
  61.  
  62. /*
  63.  * Intersect ray & list of objects.
  64.  */
  65. int
  66. ListIntersect(list, ray, hitlist, mindist, maxdist)
  67. List *list;
  68. Ray *ray;
  69. HitList *hitlist;
  70. Float mindist, *maxdist;
  71. {
  72.     Geom *objlist;
  73.     Vector vtmp;
  74.     Float s;
  75.     int hit;
  76.  
  77.     hit = FALSE;
  78.     /*
  79.      * Intersect with unbounded objects.
  80.      */
  81.     for (objlist = list->unbounded; objlist ; objlist = objlist->next) {
  82.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  83.             hit = TRUE;
  84.     }
  85.  
  86.     /*
  87.      * Check for intersection with bounding box.
  88.      */
  89.     s = *maxdist;    /* So maxdist won't be reset. */
  90.     VecAddScaled(ray->pos, mindist, ray->dir, &vtmp);
  91.     if (OutOfBounds(&vtmp, list->bounds) &&
  92.         !BoundsIntersect(ray, list->bounds, mindist, &s))
  93.         /*
  94.          * Ray never hit list.
  95.          */
  96.         return hit;
  97.     /*
  98.      * Else the ray enters list-space before it hits an
  99.      * unbounded object. Intersect with objects on list.
  100.      */
  101.     for (objlist = list->list; objlist ; objlist = objlist->next) {
  102.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  103.             hit = TRUE;
  104.     }
  105.  
  106.     return hit;
  107. }
  108.  
  109. Methods *
  110. ListMethods()
  111. {
  112.     if (iListMethods == (Methods *)NULL) {
  113.         iListMethods = MethodsCreate();
  114.         iListMethods->methods = ListMethods;
  115.         iListMethods->create = (GeomCreateFunc *)ListCreate;
  116.         iListMethods->name = ListName;
  117.         iListMethods->intersect = ListIntersect;
  118.         iListMethods->bounds = ListBounds;
  119.         iListMethods->convert = ListConvert;
  120.         iListMethods->checkbounds = FALSE;
  121.         iListMethods->closed = TRUE;
  122.     }
  123.     return iListMethods;
  124. }
  125.  
  126. void
  127. ListBounds(list, bounds)
  128. List *list;
  129. Float bounds[2][3];
  130. {
  131.     Geom *obj, *next;
  132.  
  133.     BoundsInit(list->bounds);
  134.     /*
  135.      * For each object on the list,
  136.      * compute its bounds...
  137.      */
  138.     list->unbounded  = GeomComputeAggregateBounds(&list->list, 
  139.                 list->unbounded, list->bounds);
  140.     BoundsCopy(list->bounds, bounds);
  141. }
  142.  
  143. void
  144. ListMethodRegister(meth)
  145. UserMethodType meth;
  146. {
  147.     if (iListMethods)
  148.         iListMethods->user = meth;
  149. }
  150.